<?php
/* SVN FILE: $Id: user.php 36 2009-10-22 21:25:27Z kenreily@gmail.com $ */
/**
 * Short description for file.
 *
 * Long description for file
 *
 * PHP versions 4 and 5
 *
 * Recenseo: Online submission and reviewing for education
 * Copyright 2008-2009, Ken Reily
 *
 * Licensed under The MIT License
 * Redistributions of files must retain the above copyright notice.
 *
 * $Date: 2009-10-22 21:25:27 +0000 (Thu, 22 Oct 2009) $
 * $Revision: 36 $
 * $Author: kenreily@gmail.com $
 * $HeadURL: http://recenseo.googlecode.com/svn/trunk/app/models/user.php $
 */
class User extends AppModel {
  var $name = 'User';
  var $displayField = 'username';
  var $hasMany = 'Submission';
  var $hasAndBelongsToMany = 'Course';
  
  function getIdForUsername($username)
  {
    return $this->query("SELECT id FROM users WHERE username = '{$username}'");
  }
  
  function flexDayAllowance($user_id,$course_id)
  {
    return $this->query("SELECT user_id,course_id,flex_day_allowance " .
                          "FROM courses_users AS cu " .
                         "WHERE user_id = {$user_id} " .
                           "AND course_id = {$course_id} ");
  }
  
  function flexDayUsage($user_id,$course_id)
  {
    return $this->query("SELECT s.user_id " .
                              ",a.course_id " .
                              ",a.id " .
                              ",CASE " .
                                 "WHEN DATEDIFF(s.created,a.due) < 0 THEN 0 " .
                                 "ELSE DATEDIFF(s.created,a.due) " .
                               "END AS flex_days_used " .
                          "FROM submissions AS s " .                          
                          "JOIN assignments AS a ON a.id = s.assignment_id " .
                         "WHERE s.user_id = {$user_id} " .
			   "AND s.active = 1 " . 
                           "AND a.course_id = {$course_id} ");
  }  

  function getLeaderboardData($course_id)
  {
    return    
      $this->query("SELECT User.id, User.name, avg_test_cases, avg_feedback_chars, author_feedback_score, peer_feedback_score, responsiveness, accuracy, abs(accuracy) AS abs_accuracy " .
		   "FROM users AS User " .
		   "JOIN courses_users AS CourseUser ON CourseUser.user_id = User.id " .
		   "JOIN user_statistics AS UserStatistic ON UserStatistic.user_id = User.id " .
		   "WHERE CourseUser.course_id = {$course_id} " .
		   "AND user_statistics_set_id = (SELECT MAX(id) FROM user_statistics_set)");
  }

  function getUserStats($username)
  {
    $result = 
      $this->query("SELECT user_id, avg_test_cases, avg_feedback_chars, author_feedback_score, peer_feedback_score, responsiveness, accuracy, abs(accuracy) AS abs_accuracy " .
		   "FROM users AS User " .
		   "JOIN user_statistics AS UserStatistic ON UserStatistic.user_id = User.id " .
		   "WHERE User.username = '$username' " .
		   "AND user_statistics_set_id = (SELECT MAX(id) FROM user_statistics_set) LIMIT 1");

    if (empty($result))
    {
      return $result;
    }
    else
    {
      return $result[0];
    }
  }   
}
?>